﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using debioscoop.WebUI.linq;

public partial class bestellen_Stoelkeuze : System.Web.UI.Page
{
    const int ROWCOUNT = 15;
    const int COLUMNCOUNT = 16;

    private MovieManager mm;
    private PlanningManager pm;
    private DateTimeConverter dtc = new DateTimeConverter();

    private bestelling currentOrder;
    private planning planning;
    private ICollection<kaartje> kaartjes;

    private film currentMovie;
    private int currentTicket;

    private string takenChairs = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Master.getUser() == null)
        {
            Response.Redirect("~/Login.aspx?redirect=bestellen/Stoelkeuze.aspx");
        }

        if (!String.IsNullOrEmpty(Request.QueryString["currentTicket"]))
        {
            currentTicket = int.Parse(Request.QueryString["currentTicket"]);
        }
        else
        {
            currentTicket = 0;
        }

        if (Session["order"] != null)
        {
            mm = new MovieManager((debioscoopEntities)Session["db"]);
            pm = new PlanningManager((debioscoopEntities)Session["db"]);

            currentOrder = (bestelling)Session["order"];

            planning = currentOrder.planning;
            kaartjes = currentOrder.kaartjes;
            currentMovie = planning.film;

            //Fill controls with info from the film object
            movieImg.ImageUrl = currentMovie.url;
            movTitle.Text = currentMovie.naam;
            movGenres.Text = mm.getGenreInString(currentMovie, ",", Master.tl.getCurrentLanguage());
            movLength.Text = currentMovie.duur.ToString();
            movProducer.Text = currentMovie.reggiseur;
            movType.Text = ((bool)planning.dried ? "3D" : "OV");
            movSeatsAvailable.Text = pm.getAvailableSeats(planning).ToString();
            movTime.Text = dtc.getTimeforMovie(planning.tijd);

            if (!IsPostBack)
            {
                // create a random datatable which we are going to bind to the gridview
                DataTable dt = CreateDynamicTable(planning.zaal.rijen, (int)Math.Ceiling((double)planning.zaal.zitting / (double)planning.zaal.rijen));

                // dynamically create the gridview based on the rendered datatable
                InitializeGridView(dt);

                // link the datatable to the datasource and bind the grid.
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        else
        {
            Response.Redirect("~/");
        }
    }

    private void InitializeGridView(DataTable dt)
    {
        foreach (DataColumn col in dt.Columns)
        {
            HyperLinkField field = new HyperLinkField();

            field.DataTextField = col.ColumnName;
            field.HeaderText = col.ColumnName;

            GridView1.Columns.Add(field);
        }

    }

    private DataTable CreateDynamicTable(int rows, int columns)
    {
        DataTable dt = new DataTable();

        DataColumn dac = new DataColumn("firstColumn");
        dt.Columns.Add(dac);

        for (int c = 1; c <= columns; c++)
        {
            DataColumn dc = new DataColumn("Column{0}".FormatWith(c), typeof(int));
            dt.Columns.Add(dc);
        }

        for (int r = 1; r <= rows; r++)
        {
            DataRow dr = dt.NewRow();


            foreach (DataColumn column in dt.Columns)
            {
                dr[column.ColumnName] = 0;
            }
            dt.Rows.Add(dr);
        }

        return dt;
    }

    protected void ButtonDoPostBack_Click(object sender, EventArgs e)
    {
        int row = 1;
        int chair = 1;

        string[] stoelRay = HiddenFieldSelectedValues.Value.Split('c'); //stoel
        chair = int.Parse(stoelRay[1]);

        string newHiddenValue = HiddenFieldSelectedValues.Value;
        newHiddenValue.Replace("c" + chair, "");

        HiddenFieldSelectedValues.Value = newHiddenValue;

        string[] rowRay = HiddenFieldSelectedValues.Value.Split('r');

        string chairString = "c" + chair;
        string derp = rowRay[1];

        row = int.Parse(derp.Substring(0, derp.Length - chairString.Length));


        int i = 0;
        foreach (kaartje ticket in kaartjes)
        {
            if (i == currentTicket)
            {
                Response.Write(i + " == " + currentTicket);
                ticket.rijnummer = row;
                ticket.zittingnummer = chair;

            }
            i++;
        }

        currentTicket++;
        HiddenFieldSelectedValues.Value = "";

        if (currentTicket == kaartjes.Count)
        {
            Response.Redirect("~/bestellen/Afrekenen.aspx");
        }
        else
        {
            Response.Redirect("~/bestellen/stoelkeuze.aspx?currentTicket=" + currentTicket);
        }
    }

    private kaartje getTicket(int row, int chair)
    {
        kaartje ticket = null;

        foreach (kaartje tick in planning.kaartjes)
        {
            if (tick.zittingnummer == chair && tick.rijnummer == row)
            {
                ticket = tick;
                break;
            }
        }

        if (ticket == null)
        {

            foreach (kaartje tick in kaartjes)
            {
                if (tick.zittingnummer == chair && tick.rijnummer == row)
                {
                    ticket = tick;
                    break;
                }
            }
        }

        return (ticket == null) ? new kaartje() : ticket;
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gv = sender as GridView;

            foreach (DataControlField column in gv.Columns)
            {
                string columnName = column.HeaderText;
                int columnIndex = gv.Columns.IndexOf(column);
                int rowIndex = e.Row.RowIndex + 1;

                HyperLink link = (HyperLink)e.Row.Cells[columnIndex].Controls[0];

                if (columnName.Equals("firstColumn"))
                {
                    link.NavigateUrl = "";
                    link.CssClass = "chair empty";
                    link.Text = Master.tl.getTranslation("row") + ": " + rowIndex;
                }
                else
                {
                    link.NavigateUrl = "javascript:selectedItem('r{0}c{1}');".FormatWith(rowIndex, columnIndex, link.Text);
                    if (takenChairs.Contains("r" + rowIndex + "c" + columnIndex))
                    {
                        link.CssClass = "chair taken";
                    }
                    else
                    {
                        link.CssClass = "chair";
                    }
                    link.Text = (columnIndex).ToString();
                }

                if (getTicket(rowIndex, columnIndex).rijnummer != null)
                {
                    link.CssClass = "chair taken";
                    link.NavigateUrl = "";
                }



                e.Row.CssClass = "chairRow";
            }
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        foreach (kaartje ticket in kaartjes)
        {
            ticket.rijnummer = 0;
            ticket.zittingnummer = 0;
        }

        Response.Redirect("~/bestellen/stoelkeuze.aspx");
    }
}